package com.pfzy.lib.sdk.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 权限工具
 */
final class PermissionUtils {
    PermissionUtils() { }

    public static boolean isOverMarshmallow() {
        return Build.VERSION.SDK_INT >= 23;
    }

    static boolean isOverOreo() {
        return Build.VERSION.SDK_INT >= 26;
    }

    /**
     * 清单权限
     * @param context
     * @return
     */
    public static List<String> getManifestPermissions(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            return Arrays.asList(pm.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 是否具有安装权限
     * @param context
     * @return
     */
    static boolean isHasInstallPermission(Context context) {
        if (isOverOreo()) {
            return context.getPackageManager().canRequestPackageInstalls();
        }
        return true;
    }

    /**
     * 悬浮窗权限
     * @param context
     * @return
     */
    static boolean isHasOverlaysPermission(Context context) {
        if (isOverMarshmallow()) {
            return Settings.canDrawOverlays(context);
        }
        return true;
    }

    /**
     * 文件权限
     * @param context
     * @param permissions2
     * @return
     */
    public static ArrayList<String> getFailPermissions(Context context, List<String> permissions2) {
        if (!isOverMarshmallow()) {
            return null;
        }
        ArrayList<String> failPermissions = null;
        for (String permission : permissions2) {
            if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED) {
                if (failPermissions == null) {
                    failPermissions = new ArrayList<>();
                }
                failPermissions.add(permission);
            }
        }
        return failPermissions;
    }

    /**
     * 是请求被拒绝的权限
     * @param activity
     * @param failPermissions
     * @return
     */
    static boolean isRequestDeniedPermission(Activity activity, List<String> failPermissions) {
        for (String permission : failPermissions) {
            if (!checkSinglePermissionPermanentDenied(activity, permission)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 选中“永久拒绝更多权限”
     * @param activity
     * @param permissions2
     * @return
     */
    public static boolean checkMorePermissionPermanentDenied(Activity activity, List<String> permissions2) {
        for (String permission : permissions2) {
            if (checkSinglePermissionPermanentDenied(activity, permission)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 选中“永久拒绝单一权限”
     * @param activity
     * @param permission
     * @return
     */
    static boolean checkSinglePermissionPermanentDenied(Activity activity, String permission) {
        return isOverMarshmallow() && activity.checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED &&
                !activity.shouldShowRequestPermissionRationale(permission);
    }

    /**
     * 失败权限
     * @param permissions2
     * @param grantResults
     * @return
     */
    public static List<String> getFailPermissions(String[] permissions2, int[] grantResults) {
        List<String> failPermissions = new ArrayList<>();
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] == -1) {
                failPermissions.add(permissions2[i]);
            }
        }
        return failPermissions;
    }

    /**
     * 成功权限
     * @param permissions2
     * @param grantResults
     * @return
     */
    public static List<String> getSucceedPermissions(String[] permissions2, int[] grantResults) {
        List<String> succeedPermissions = new ArrayList<>();
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] == 0) {
                succeedPermissions.add(permissions2[i]);
            }
        }
        return succeedPermissions;
    }

    /**
     * 检查权限
     * @param activity
     * @param requestPermissions
     */
    public static void checkPermissions(Activity activity, List<String> requestPermissions) {
        List<String> manifest = getManifestPermissions(activity);
        if (manifest != null && manifest.size() != 0) {
            for (String permission : requestPermissions) {
                if (!manifest.contains(permission)) {
                    throw new ManifestRegisterException(permission);
                }
            }
            return;
        }
        throw new ManifestRegisterException(null);
    }

    /**
     * 检查目标软件版本
     * @param context
     * @param requestPermissions
     */
    public static void checkTargetSdkVersion(Context context, List<String> requestPermissions) {
        if (context.getApplicationInfo().targetSdkVersion < 23) {
            throw new RuntimeException("The targetSdkVersion SDK must be 23 or more");
        }
    }
}
